$(function () {
   obj.init();
});

var obj =  new function () {
   'use strict';
    return {
        groupTree:null,
        sblId:null,
        currentId:null,
        init:function () {
            this.sblId  = xhTool.getQuery('iSBLId');//校本库id
            this.groupTree = $('#groupList');
            $('.xh-datagrid-toolbar').parseToolbar();
            this.funObj().initTreeEvent();
            this.listener();

        },
        listener:function () {
            var $this = this;
            //新增按钮点击事件
            $('#bt_list_add').click(function () {
                var select = $this.groupTree.treegrid('getSelections')[0];
                if(select != undefined){
                    console.log(select);
                    if(select['iLevel'] == 2){
                        window.top.xhTop.showError('错误','不能创建小组里的子小组');
                        return false;
                    }
                    window.top.xhTop.showModal({
                        title:'新增组织',
                        content: '<iframe src="'+baseUrl+'school_book/groupForm/'+$this.sblId+'/'+select['iSBGId']+'"  style="width: 100%;;border-style: none;padding: 0px;height: 220px"  scrolling="yes"   ></iframe>',
                        button:'提交',
                        backClose: false
                    })
                }else {
                    window.top.xhTop.showModal({
                        title:'新增组织',
                        content: '<iframe src="'+baseUrl+'school_book/groupForm/'+$this.sblId+'"  style="width: 100%;;border-style: none;padding: 0px;height: 220px"  scrolling="yes"   ></iframe>',
                        button:'提交',
                        backClose: false
                    })
                }
            });

            //删除
            $('#bt_list_del').click(function () {
                var selects = $this.groupTree.treegrid('getSelections');
                if(selects.length <= 0){
                    window.top.xhTop.showError('','没有选中');
                    return false;
                }
                var filed = [];
                $.each(selects,function (i,item) {
                    filed.push(item.iSBGId);
                });
                xhExtra.askChangeStatus('school_book/delGroup',{ids:filed},null,null,function (res) {
                    window.top.xhTop.showSuccess(res.msg);
                    $this.groupTree.treegrid('reload');
                });
            });

            //快捷创建
            $('#bt_list_addAll').click(function () {
                window.top.xhTop.showModal({
                    title:'快捷创建',
                    content: '<iframe src="'+baseUrl+'school_book/addTotalGroup/'+$this.sblId+'"  style="width: 100%;;border-style: none;padding: 0px;height: 320px"  scrolling="yes"   ></iframe>',
                    button:'提交',
                    backClose: false,
                    size:'lg',
                    width:900
                })
            });
        },
        funObj:function () {
            var  $this = this;
            return {
                initTreeEvent:function () {
                    $this.groupTree.treegrid({
                        onSelect:function (row) {
                            $this.currentId=row['iSBGId'];
                            $this.groupTree.treegrid('expand',$this.currentId);
                        },
                        onBeforeSelect:function (row) {
                            if($this.currentId==null){
                                return true;
                            }else {
                                if($this.currentId==row['iSBGId']){
                                    $this.groupTree.treegrid('unselect',$this.currentId);
                                    $this.groupTree.treegrid('collapse',$this.currentId);
                                    $this.currentId=null;
                                    return false;
                                }
                            }
                        },
                        onBeforeCheck:function (row) {
                            if($this.currentId==null){
                                return true;
                            }else {
                                if($this.currentId==row['iSBGId']){
                                    $this.groupTree.treegrid('uncheckRow',$this.currentId);
                                    currentDepId=null;
                                    return false;
                                }
                            }
                        },
                        onDblClickRow:function (row) { //双击事件
                            var iSBGId = row['iSBGId'];
                            window.top.xhTop.showModal({
                                title:'编辑组织',
                                content: '<iframe src="'+baseUrl+'school_book/groupForm/'+$this.sblId+'?iSBGId='+iSBGId+'"  style="width: 100%;;border-style: none;padding: 0px;height: 220px"  scrolling="yes"   ></iframe>',
                                button:'提交',
                                backClose: false
                            })
                        }
                    });
                }
            }
        }
   }
};